﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Lab6_Product
{
    public partial class frmEditProduct : Form
    {
        string imagePath;
        int productID;
        
        //string imagePath = "";
        string name = "";
        string des = "";
        int priceIn = 0;
        int priceSell = 0;
        int quantity = 0;
        public frmEditProduct()
        {
            InitializeComponent();
        }

        private void txtPriceSell_TextChanged(object sender, EventArgs e)
        {

        }

        private void frmEditProduct_Load(object sender, EventArgs e)
        {
           
         
        }

        public void LoadForm( int id)
        {
            
            try
            {

               
           DataProccess dp = new DataProccess();
           Product p=  dp.GetData(id);
           txtID.Text = p.Id.ToString();
           txtName.Text = p.ProductName;
           txtDes.Text = p.Description;
           txtPriceIn.Text = p.PriceIn.ToString();
           txtPriceSell.Text = p.PriceSell.ToString();
           txtQuantity.Text = p.Quantity.ToString();
           string imgP= p.Image;

           //load image to Picture box
           try
           {
               if (!"".Equals(imgP))
               {
                   string exeFolder = Application.StartupPath;
                   string path1 = exeFolder.Substring(0, exeFolder.LastIndexOf(@"\"));
                   string path2 = path1.Substring(0, path1.LastIndexOf(@"\"));
                   string imgPath = Path.Combine(path2, @"image\ava.jpg");
                   Image img = Image.FromFile(imgP);
                   pictureBox1.Image = img;
                   pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
               }
           }
           catch (FileNotFoundException ie)
           {
               MessageBox.Show(ie.Message);
           }
           
           productID = id;

            }
            catch (Exception e)
            {

                 MessageBox.Show(e.ToString());
            }

            frmViewAllProducts f = new frmViewAllProducts();
            f.enableFunction();
            
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                if (c.Enabled == false)
                {
                    c.Enabled = true;
                }
            }
            btnImage.Show();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (check())
            {
                productID = Convert.ToInt32(txtID.Text);
                name = txtName.Text;
                des = txtDes.Text;
                priceIn = Convert.ToInt32(txtPriceIn.Text);
                priceSell = Convert.ToInt32(txtPriceSell.Text);
                quantity = Convert.ToInt32(txtQuantity.Text);
                DataProccess dp = new DataProccess();
                
                bool result = dp.EditProduct(productID, txtName.Text, txtDes.Text, Convert.ToInt32(txtPriceIn.Text), Convert.ToInt32(txtPriceSell.Text), Convert.ToInt32(txtQuantity.Text), imagePath);
                if (result)
                {
                    MessageBox.Show("Edit success!");

                }
            }
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string file = openFileDialog1.FileName;
                    string fileName = file.Substring(file.LastIndexOf(@"\"), file.Length - file.LastIndexOf(@"\"));
                    //Image img = Image.FromFile(@file);

                    string exeFolder = Application.StartupPath;
                    string path1 = exeFolder.Substring(0, exeFolder.LastIndexOf(@"\"));
                    string path2 = path1.Substring(0, path1.LastIndexOf(@"\"));
                    string imgPath = Path.Combine(path2, @"image" + fileName);
                    byte[] b = null;
                    using (FileStream f = new FileStream(@file, FileMode.Open))
                    {
                        b = new byte[f.Length];

                        f.Read(b, 0, b.Length);
                    }

                    // Write to file ...
                    using (FileStream fs = new FileStream(@imgPath, FileMode.Create))
                    {
                        fs.Write(b, 0, b.Length);

                    }
                    Image img = Image.FromFile(imgPath);
                    pictureBox1.Image = img;
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    if (!"".Equals(imgPath))
                    {
                        imagePath = imgPath;
                    }
                    else imagePath = "";
                    




                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
           DialogResult re= MessageBox.Show("Do u really want to delete this product?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (re==DialogResult.Yes)
            {
                DataProccess dp = new DataProccess();
                if (dp.Delete(productID))
                {
                    dp.UpdateAfterDelete(productID);
                    MessageBox.Show("Delete Success!");
                }
            }
            
        }

        public bool check()
        {
            if (txtID.Text.Length == 0)
            {
                MessageBox.Show("Why u clear ID, ur ID is " + productID + ", input it again -.- ");
                txtID.Focus();
                return false;

            }
            else if (txtName.Text.Length == 0)
            {
                MessageBox.Show("Are u kidding me! A product without name, is it an invisible product?");
                txtName.Focus();
                return false;
            }
            else if (txtPriceIn.Text.Length == 0)
            {
                DialogResult pInDialog = MessageBox.Show("Is your product really free? Congrat, u will get a lot cash when u sell it!", "Really?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (pInDialog == DialogResult.No)
                {
                    txtPriceIn.Focus();
                    return false;
                }
                else
                {
                    txtPriceIn.Text = "0";
                    return true;
                }
            }
            else if (txtPriceSell.Text.Length == 0)
            {
                MessageBox.Show("Want to sell your product FREE? I guess u just forget input Selling Price", "Selling Price", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPriceSell.Focus();
                return false;
            }
            else if (txtQuantity.Text.Length == 0)
            {
                MessageBox.Show("Please input quantity");
                txtQuantity.Focus();
                return false;
            }
            else return true;
        }
    }
}
